﻿<cfsilent>
	<cfscript>

		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");
		mathAdvice = getProperty("serviceFactory").getBean("senateMathAdvice");
		
		response = structNew();
		structInsert(response, "success", false, true);
		
		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		rebuildPart = event.getArg("PAR");
		
		/* Every SQL operation is TRANSACATION*/
		transaction {
			
			
			/* 第一步 载入教学任务 */
			switch( taskType ){
				
				case "P":
					sql = "SELECT 
								a.term_id, a.cidx, a.tch_id, a.task_week, a.task_period, a.task_time, a.task_place, 
								b.cid, b.course_name 
								FROM t_task a 
									INNER JOIN t_course b ON a.cid = b.cid 
								WHERE 
									a.tsk_id = :taskId ";
					break;

				case "C":
					sql = "SELECT 
								a.term_id, a.cidx, a.tch_id, a.task_week, a.task_period, a.task_time, a.task_place, 
								b.cid, b.course_name 
								FROM t_common_task a 
									INNER JOIN t_course b ON a.cid = b.cid 
								WHERE 
									a.tsk_id = :taskId ";
					break;

				case "R":
					sql = "SELECT 
								a.term_id, a.cidx, a.tch_id, a.task_week, a.task_period, a.task_time, a.task_place, 
								b.cid, b.course_name 
								FROM t_reopen_task a 
									INNER JOIN t_course b ON a.cid = b.cid 
								WHERE 
									a.tsk_id = :taskId ";
					break;

				case "T":
					sql = "SELECT 
								a.term_id, a.cidx, a.tch_id, a.task_week, a.task_period, a.task_time, a.task_place, 
								b.cid, b.course_name 
								FROM t_sport_task a 
									INNER JOIN t_course b ON a.cid = b.cid 
								WHERE 
									a.tsk_id = :taskId ";
					break;
			}
			
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
			rs_task = queryObj.execute( sql=sql ).getResult();
			
			
			/* 
			=============================================
				重新计算已排学时 
			=============================================
			*/
			
			/* 计算周次序列和已排周学时 */
			taskWeek = listToArray( rs_task.task_week, "-" );
			periodWeeks = int( taskWeek[2] ) - int( taskWeek[1] ) + 1;
			taskTimeArray = listToArray( rs_task.task_time, "," );
			
			periodDone = 0;
			
			for ( taskTime in taskTimeArray ) {
				cells = listGetAt( taskTime, 3, "@-");
				/* 累计已排周学时 */
				periodDone += periodWeeks * cells;
			}
			
			sql = "UPDATE t_task_arrange SET period_done = :periodDone WHERE task_id = :taskId ";
						
			queryObj = new query( datasource=application.dnsMaster );
			queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			queryObj.addParam( name="periodDone", value=periodDone, cfsqltype="cf_sql_interger" );
			queryObj.execute( sql=sql );
			
			
			operation = rs_task.course_name & "-" & rs_task.cidx & " 重建 ";
			
			
			/* 
			=============================================
				重建教师时间占用表 
			=============================================
			*/
			if ( listFind( rebuildPart, "TEACHER_TIMETABLE", "," ) ) {
				
				/* 删除现有占用表 */
				sql = "DELETE FROM t_teacher_timetable WHERE task_id = :taskId ";
				
				queryObj = new query( datasource=application.dnsMaster );
				queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
			
				queryObj.execute( sql=sql );
				
				
				/* 重新生成时间占用表 */
				taskTimeArray = listToArray( rs_task.task_time, "," );
				taskPlaceArray = listToArray( rs_task.task_place, "," );

				termId = rs_task.term_id; 
				teacherId = rs_task.tch_id; 
				courseId = rs_task.cid;
				taskWeek = rs_task.task_week;

				sequence = getProperty("serviceFactory").getBean("sequenceTeacherTimeTable");
				sequence.setTermID( termId );
				sequence.setTeacherID( teacherId );
				
				weekIdx = mathAdvice.convertWeekIdx( taskWeek );
				
				for ( i=1; i LTE arrayLen(taskTimeArray); i++ ) {
					
					/* 当前讲次 */
					taskTime = taskTimeArray[i];
					taskPlace = "";
					
					/* 当前将次地点 */
					if ( i LTE arrayLen(taskPlaceArray) ) {
						taskPlace = taskPlaceArray[i];
					}
					
					weekDay = listGetAt( taskTime, 1, "@-");
					lectureId = listGetAt( taskTime, 2, "@-");
					cells = listGetAt( taskTime, 3, "@-");
					
					cellStart = mathAdvice.convertLectureIdxToCellStart( lectureId );

					teacherTimeIdx = sequence.getTimeIDX();
					
					sequence.setTimeIndex( teacherTimeIdx );

					teacherTimeId = sequence.getID();
					
					
					sql = "INSERT INTO t_teacher_timetable 
							(
								teachertime_id, term_id, tch_id, cid, tidx, widx, wdidx, cellidx, cellrelay, taskclsroom, tasktime, task_id, task_type, task_week 
							) 
							VALUES 
							(
								:teacherTimeId, 
								:termId, :teacherId, :courseId, :teacherTimeIdx, :weekIdx, :weekDay, :cellStart, :cells, 
								:taskPlace, :taskTime, 
								:taskId, :taskType, :taskWeek
							) ";
					
					queryObj = new query( datasource=application.dnsMaster );
					queryObj.addParam( name="teacherTimeId", value=teacherTimeId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="teacherId", value=teacherId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="courseId", value=courseId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="teacherTimeIdx", value=teacherTimeIdx, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="weekIdx", value=weekIdx, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="weekDay", value=weekDay, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="cellStart", value=cellStart, cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="cells", value=cells, cfsqltype="cf_sql_integer" );
					queryObj.addParam( name="taskPlace", value=taskPlace, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="taskTime", value=taskTime, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="taskId", value=taskId, cfsqltype="cf_sql_varchar" );
					queryObj.addParam( name="taskType", value=taskType, cfsqltype="cf_sql_char" );
					queryObj.addParam( name="taskWeek", value=taskWeek, cfsqltype="cf_sql_char" );
			
					queryObj.execute( sql=sql );
					
				}

				operation = operation & " 教师时间占用表 ";
			
			}
			
			structInsert(response, "operation", operation, true);
			structInsert(response, "success", true, true);

		}

	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>